Page({
  data: {
    userInfo: {
      isLoggedIn: false,
      name: '',
      avatar: '',
      id: ''
    }
  },

  onLoad() {
    this.fetchUserInfo()
  },

  fetchUserInfo() {
    // 模拟获取用户信息
    this.setData({
      userInfo: {
        isLoggedIn: true,
        name: '校园用户',
        avatar: './static/user-avatar.jpg',
        id: '20230001'
      }
    })
  },

  handleLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  handleLogout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出当前账号吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            userInfo: {
              isLoggedIn: false,
              name: '',
              avatar: '',
              id: ''
            }
          })
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          })
        }
      }
    })
  },

  navigateTo(e) {
    const page = e.currentTarget.dataset.page
    const routeMap = {
      favorites: '/pages/favorites/favorites',
      history: '/pages/history/history',
      notifications: '/pages/notifications/notifications',
      settings: '/pages/settings/settings',
      about: '/pages/about/about',
      feedback: '/pages/feedback/feedback'
    }
    wx.navigateTo({
      url: routeMap[page]
    })
  }
})
